package cn.stylefeng.guns.modular.event.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.event.entity.Event;
import cn.stylefeng.guns.modular.event.mapper.EventMapper;
import cn.stylefeng.guns.modular.event.model.params.EventParam;
import cn.stylefeng.guns.modular.event.model.result.EventResult;
import  cn.stylefeng.guns.modular.event.service.EventService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 事件 服务实现类
 * </p>
 *
 * @author CHENCHEN
 * @since 2022-07-19
 */
@Service
public class EventServiceImpl extends ServiceImpl<EventMapper, Event> implements EventService {

    @Override
    public void add(EventParam param){
        Event entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(EventParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(EventParam param){
        Event oldEntity = getOldEntity(param);
        Event newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public EventResult findBySpec(EventParam param){
        return null;
    }

    @Override
    public List<EventResult> findListBySpec(EventParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(EventParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(EventParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Event getOldEntity(EventParam param) {
        return this.getById(getKey(param));
    }

    private Event getEntity(EventParam param) {
        Event entity = new Event();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
